package com.hjr.leetcode.util.test;

import java.util.ArrayList;
import java.util.LinkedList;

public class listXn {

    //测试list的读写性能
    public static void main(String[] args) {
        testArrayList();
        testLinkedList();
    }

    public static void testArrayList() {
        //一千万个元素
        long l = System.currentTimeMillis();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        list.add(100);
        System.out.println("ArrayList 添加耗时：" + (System.currentTimeMillis() - l));

        //找出相同的元素
        l = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            for (int j = i + 1; j < 10000; j++) {
                if (list.get(i).equals(list.get(j))){
                    System.out.println("找到相同的元素：" + list.get(i));
                }
            }
        }
        System.out.println("ArrayList 遍历耗时：" + (System.currentTimeMillis() - l));
    }

    public static void testLinkedList() {
        //一千万个元素
        long l = System.currentTimeMillis();
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        list.add(100);
        System.out.println("LinkedList 添加耗时：" + (System.currentTimeMillis() - l));

        //找出相同的元素
        l = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            for (int j = i + 1; j < 10000; j++) {
                if (list.get(i).equals(list.get(j))){
                    System.out.println("找到相同的元素：" + list.get(i));
                }
            }
        }
        System.out.println("ArrayList 遍历耗时：" + (System.currentTimeMillis() - l));
    }

}
